import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class CustomerFrame implements ActionListener,KeyListener {      //keyListener键盘上敲击
    private final JFrame frame = new JFrame("连接五子棋游戏");
    private final JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 55, 10));
    private final JPanel optionPanel = new JPanel(new BorderLayout(10, 10));
    private final JPanel inputPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 5));
    private final JPanel hostPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 5));
    private final JButton startBtn = new JButton("连接游戏");
    private final JButton stopBtn = new JButton("断开游戏");
    private final JLabel nameLabel = new JLabel("您的名称:");
    private final JLabel hostLabel = new JLabel("服务器IP:");
    private final JTextArea textArea = new JTextArea(10,30);     //创建文本区域
    private final JTextField nameField = new JTextField("",15);   // 创建文本输入框
    private final JTextField hostField = new JTextField("localhost",20);

    private Client client;  //自己定义的类
    private Thread clientThread;

    private void initUI() {
        frame.setSize(600, 400);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   //关闭窗口会结束
        frame.setLayout(new BorderLayout());      // 设置
        frame.setResizable(false);  // 禁止窗口调整大小
        frame.setAlwaysOnTop(true);  // 窗口始终置顶

        Dimension btnSize = new Dimension(110, 35); // 宽度150px，高度50px
        buttonPanel.setVisible(true);

        optionPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));


        optionPanel.setVisible(true);

        // 添加到窗口底部
        frame.add(buttonPanel, BorderLayout.SOUTH);
        frame.add(optionPanel, BorderLayout.CENTER);

        startBtn.setBackground(Color.GRAY);
        startBtn.setFont(new Font("宋体", Font.PLAIN, 15));
        startBtn.setPreferredSize(btnSize);
        startBtn.setActionCommand("startBtn");
        startBtn.addActionListener(this);
        startBtn.setVisible(true);
        startBtn.setEnabled(true);
        buttonPanel.add(startBtn);

        stopBtn.setBackground(Color.GRAY);
        stopBtn.setFont(new Font("宋体", Font.PLAIN, 15));
        stopBtn.setPreferredSize(btnSize);  //按钮大小
        stopBtn.setActionCommand("stopBtn");
        stopBtn.addActionListener(this);
        stopBtn.setVisible(true);
        stopBtn.setEnabled(false);
        buttonPanel.add(stopBtn);

        nameLabel.setFont(new Font("宋体",Font.PLAIN,15));
        nameLabel.setVisible(true);
        nameLabel.setPreferredSize(new Dimension(100,30));
        inputPanel.add(nameLabel);

        nameField.setColumns(32);
        nameField.setFont(new Font("宋体", Font.PLAIN, 22));
        nameField.setVisible(true);
        inputPanel.add(nameField);

        optionPanel.add(inputPanel, BorderLayout.NORTH);

        hostLabel.setFont(new Font("宋体",Font.PLAIN,15));
        hostLabel.setVisible(true);
        hostLabel.setPreferredSize(new Dimension(100,30));
        hostPanel.add(hostLabel);

        hostField.setColumns(32);
        hostField.setFont(new Font("宋体", Font.PLAIN, 22));
        hostField.setVisible(true);
        hostPanel.add(hostField);

        optionPanel.add(hostPanel,BorderLayout.CENTER);


        textArea.setFont(new Font("宋体", Font.PLAIN, 14));
        JScrollPane scrollPane = new JScrollPane(textArea);
        optionPanel.add(scrollPane, BorderLayout.SOUTH);

        frame.setVisible(true);

    }

    public CustomerFrame() {
        initUI();
    }

    private void clickStartBtn() {
        client = new Client(textArea,hostField,nameField,frame);
        clientThread = new Thread(client);

        clientThread.start();

        client.doSubmit();

        startBtn.setEnabled(false);
        stopBtn.setEnabled(true);
    }

    private void clickStopBtn() {
        clientThread.interrupt();

        try {
            // 等到线程完全结束再执行之后的操作
            clientThread.join();
        } catch (InterruptedException e) {
            textArea.append(e.getMessage());
        }

        startBtn.setEnabled(true);
        stopBtn.setEnabled(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        switch (e.getActionCommand()) {
            case "startBtn":
                clickStartBtn();
                break;
            case "stopBtn":
                clickStopBtn();
                break;

        }
    }

    public static void main(String[] args) {
        new CustomerFrame();
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {

    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}

